import json
import os
from django.http import HttpResponse, JsonResponse
from Configure.models import Configure
def uploadImg(request):
    global destination
    file = request.FILES.get('file', None)
    if not file:
        return HttpResponse("no file ")
    type=request.GET.get("type")
    if type=="PBpower":
        destination = open(os.path.join("files/jobIcon", "PBpower.png"), "wb+")
    if type=="Solar":
        destination = open(os.path.join("files/jobIcon", "Solar.png"), "wb+")
    for chunk in file.chunks():
        destination.write(chunk)
    destination.close()
    return HttpResponse("ok")

def getConfigure(request):
    pb=Configure.objects.filter(id=1)[0]
    solar=Configure.objects.filter(id=2)[0]
    return JsonResponse({"pb":{
        "iconUrl":pb.iconUrl,
        "look":pb.look,
        "salary":pb.salary,
        "payType":pb.payType,
        "joyType":pb.joyType,
        "applyLink":pb.applyLink,
    },
    "solar":{
        "iconUrl": solar.iconUrl,
        "look": solar.look,
        "salary": solar.salary,
        "payType": solar.payType,
        "joyType": solar.joyType,
        "applyLink": solar.applyLink,
    }})

def update(request):
    body = json.loads(request.body)
    pb=body["pb"]
    solar=body["solar"]
    pb_model = Configure.objects.filter(id=1)
    solar_model = Configure.objects.filter(id=2)
    pb_model.update(look=pb["look"],salary=pb["salary"],payType=pb["payType"],joyType=pb["joyType"],applyLink=pb["applyLink"])
    solar_model.update(look=solar["look"],salary=solar["salary"],payType=solar["payType"],joyType=solar["joyType"],applyLink=solar["applyLink"])
    return HttpResponse("")
